// pages/user/erji//myAddress/myAddress.js
var base = require('../../../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数
const PAGES = require('../../../../utils/pages.js')
const { HTTP } = require('../../../../lib/http.js')
const { URL } = require('../../../../lib/urls.js')
const { getUserToken, getUserInfo } = require('../../../../lib/user.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: [],
    dataList: [],
    isShowNullView: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _t: base._t(), //翻译
    });
    let that = this
    console.log("options+++++++++++", options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getNetAddressList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getNetAddressList();
  },

  //获取地址列表
  getNetAddressList: function () {
    let that = this;
    HTTP({
      url: URL.address.list,
      userToken: getUserToken(),
      data: {
      },
      success: (ress) => {
        var res=ress.Data;
        console.log("hlw:" + res)
        if (res.AddressInfos.length > 0) {
          let list = [];
          for (var index in res.AddressInfos) {
            var model = res.AddressInfos[index];
            model.address = res.AddressInfos[index].Provice + res.AddressInfos[index].City + res.AddressInfos[index].Area + res.AddressInfos[index].DetailAddress;
            list.push(model);
          }
          this.setData({
            dataList: list,
            isShowNullView:false,
          });
        } else {
          this.setData({
            dataList: [],
            isShowNullView:true,
          })
        }
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 添加地址
   */
  addAddressTap: function () {
    wx.navigateTo({
      url: PAGES.addOrEditAddress
    })
  },
  selectTap: function (e) {
    let index = e.currentTarget.dataset.index;//
    var item = this.data.dataList[index];
    //订单页面，选择收货地址
    var pages = getCurrentPages();//当前页面栈
    if (pages.length > 1) {
      var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
      beforePage.setData({
        addressInfo: item,
      });//触发父页面中的方法
      beforePage.getNetOrderInfo();
    }
    wx.navigateBack({
      delta: 1
    })
  },
  changAddressTap: function (e) {
    let index = e.currentTarget.dataset.index;//
    var item = this.data.dataList[index];
    let data = JSON.stringify(item);
    wx.navigateTo({
      url: PAGES.addOrEditAddress + '?data=' + encodeURIComponent(data)
    })
  },
  // 设置默认地址
  changDefAddTap: function (e) {
    let index = e.currentTarget.dataset.index;//
    var item = this.data.dataList[index];
    var that=this;
    HTTP({
      url: URL.address.addOrChang,
      userToken: getUserToken(),
      data: {
        AddressCode: item.AddressCode,
        IsDefault: 1,
        MemberId: getUserInfo().memberId,
      },
      success: (res) => {
        wx.showToast({
          title: '设置成功',
        })
        var list=[];
        for(var i=0;i<that.data.dataList.length;i++){
          var model = that.data.dataList[i];
          model.IsDefault=index==i?1:0
          list.push(model);
        }
        that.setData({
          dataList:list,
        });
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
})